package com.example.demo.day0303;

import com.example.demo.day0302.Student;
import java.lang.reflect.*;
import java.lang.reflect.Constructor;
public class DemoReflect4 {
    public static void main(String[] args) {
        try {
            Class clazz = Class.forName("com.example.demo.day0302.Student");

            Constructor con = clazz.getConstructor();//获取构造方法

            Student stu = (Student) con.newInstance();//获取对象

            Field f = clazz.getField("school");//获取名称为school的属性

            f.set(stu,"secondhighschool");//设置stu对象中school属性的值

            String school = (String) f.get(stu);//获取stu对象的school属性的值

            System.out.println(school);

            Field f1= clazz.getDeclaredField("privateFiled");//获取私有属性的值
            f1.setAccessible(true);//解除私有封装，之后可以强制调用
            f1.set(stu,"private");//获取stu对象的privateFiled的属性值
            System.out.println(f1.get(stu));



        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
